package com.lychee.voicetranscribe;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, AudioRevicer.OnAudioStatusListener {

    @BindView(R.id.mImgStart)
    ImageView mImgStart;
    private String TAG = getClass().getSimpleName();
    private Context mContext;
    @BindView(R.id.mTvStatus)
    TextView mTvStatus;
    private String[] autoPermission = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO};
    private AudioRevicer mAudioRevicer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mContext = this;
        mImgStart.setOnTouchListener(this);
        mAudioRevicer = new AudioRevicer();
        mAudioRevicer.setOnAudioStatusListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            //按下
            case MotionEvent.ACTION_DOWN:
                if (EasyPermissions.hasPermissions(mContext, autoPermission)) {
                    Log.i(TAG, "onTouch: 开始录制");
                    mAudioRevicer.startRecord();
                }
                break;
            //抬起
            case MotionEvent.ACTION_UP:
                mAudioRevicer.cancelRecord();
                Log.i(TAG, "onTouch: 停止录制");
                mTvStatus.setText("没有录制");
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void currStatus(AudioData audioData) {
        mTvStatus.setText("当前最高振幅" + audioData.getMaxAmplitude() + "\t当前分贝" + audioData.getDb() + "\t当前时间" + audioData.getTime());
    }
}
